import { get, post, del } from '../../utils/request'

export interface KnowledgeCategory {
    id: string
    name: string
    // 0企业知识库 1 个人知识库
    space_type: number
    user_id: string
    parent_id: string
    children: KnowledgeCategory[]
}

export interface Doctype {
    id: string
    key: string
    value: string
}

export function getMyKnowledgeCategory() {
    return new Promise<KnowledgeCategory>((resolve, reject) => {
        get<KnowledgeCategory>(`/api/knowledge/base/per/list`).then((res) => {
            console.debug('获取知识库分类成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('获取知识库分类失败', error);
            reject(error);
        });
    });
}

export function  addMyKnowledgeCategory(data: any) {
    const temp: Record<string, any> = {}
    if(data.parentId != '') {
        temp['name'] = data.name;
        temp['parent_id'] = data.parentId;
    }
    else temp['name'] = data.name;
    console.debug(temp)
    return new Promise<KnowledgeCategory>( (resolve, reject) => {
        post<KnowledgeCategory>(`/api/knowledge/base/per/create`, temp).then((res) => {
            console.debug('增加知识库分类成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('知识库添加失败', error);
            reject(error);
        });
    });
}

export function getOrgKnowledgeCategory() {
    return new Promise<any>((resolve, reject) => {
        post<any>(`/api/vkdb/get_document_hierarchy`).then((res) => {
            console.debug('获取知识库分类成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('获取知识库分类失败', error);
            reject(error);
        });
    });
}

export function delOrgKnowledgeCategory(type_id: string) {
    return new Promise<KnowledgeCategory>((resolve, reject) => {
        post<KnowledgeCategory>(`/api/vkdb/delete_document_type?type_id=${type_id}`).then((res) => {
            console.debug('删除知识库分类成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('删除知识库分类失败', error);
            reject(error);
        });
    });
}

export interface AddDocumentTypeParams {
    kb_type_name: string
    parent_id: string
    description: string
}

export function  addOrgKnowledgeCategory(data: AddDocumentTypeParams) {
    return new Promise<KnowledgeCategory>( (resolve, reject) => {
        post<KnowledgeCategory>(`/api/vkdb/add_document_type`, data).then((res) => {
            console.debug('增加知识库分类成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('知识库添加失败', error);
            reject(error);
        });
    });
}

export function updateKnowledgeCategory(data: any) {
    return new Promise<KnowledgeCategory>((resolve, reject) => {
        post<KnowledgeCategory>(`/api/knowledge/base/org/update`, data).then((res) => {
            console.debug('更新知识库分类成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('更新知识库分类失败', error);
            reject(error);
        });
    })
}

export function getKnowledgeDoctype() {
    return new Promise<Doctype[]>((resolve, reject) => {
        get<Doctype[]>(`/api/knowledge/base/doctype`).then((res) => {
            console.debug('获取知识库文档类型成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('获取知识库文档类型失败', error);
            reject(error);
        });
    })
}

export function uploadKnowledgeOrg(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    return new Promise<KnowledgeCategory>((resolve, reject) => {
        post<KnowledgeCategory>(`/api/knowledge/base/org/upload`, formData).then((res) => {
            console.debug('上传知识库文档成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('上传知识库文档失败', error);
            reject(error);
        });
    })
}

// 企业知识库列表接口
export interface KnowledgeListParams {
    file_category?: string
    description?: string
    knowledge_type?: string
    file_name?: string
    page: number
    page_size: number
}

export interface KnowledgeListItem {
    id: string
    filename: string
    chineseName: string
    fileType: string
    // 企业知识库字段
    label?: string[] | string
    knowledge_keyword?: string[] | string
    // 个人知识库字段
    tags?: string[] | string
    keywords?: string[] | string
    uploader: string
    createTime: string
    thumbnail: string
    categoryId: string
    // 其他可能存在的字段
    file_name?: string
    document_type?: string
    create_time?: string
}

export interface KnowledgeListResponse {
    data: KnowledgeListItem[]
    total: number
    page: number
    page_size: number
}

export function getKnowledgeByList(params: KnowledgeListParams) {
    return new Promise<KnowledgeListResponse>((resolve, reject) => {
        post<KnowledgeListResponse>(`/api/vkdb/get_knowledge_by_list`, params).then((res) => {
            console.debug('获取企业知识库列表成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('获取企业知识库列表失败', error);
            reject(error);
        });
    });
}

// 文件上传接口
export interface UploadFileResponse {
    success: boolean
    message: string
    data?: any
}

export function uploadFile(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    
    return new Promise<UploadFileResponse>((resolve, reject) => {
        post<UploadFileResponse>(`/api/tos/tos_up`, formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }).then((res) => {
            console.debug('文件上传成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('文件上传失败', error);
            reject(error);
        });
    });
}

// 上传知识库文档接口
export interface UploadKnowledgeParams {
    document_type: string
    key_word: string[]
    scope: string
    url: string
    file_name: string
    tos_path: string
    label: string[]
    meta_id: string
    description: string
    file_category: string
    knowledge_type?: string
}

export interface UploadKnowledgeResponse {
    success: boolean
    message: string
    data?: any
}

export function uploadKnowledge(params: UploadKnowledgeParams) {
    return new Promise<UploadKnowledgeResponse>((resolve, reject) => {
        post<UploadKnowledgeResponse>(`/api/vkdb/up_knowledge_base`, params).then((res) => {
            console.debug('上传知识库文档成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('上传知识库文档失败', error);
            reject(error);
        });
    });
}

// 知识图谱接口
export interface GraphNode {
    children: GraphNode[]
    describe: string
    entityId: string
    name: string
    path: string
    pid: string
    showFlag: string
    tag: string
}

export interface GraphData {
    graph: GraphNode
    tree: GraphNode
}

export interface GraphResponse {
    code: number
    data: GraphData
    message: string
    status: number
    tranceId: string
}

export function getGraph() {
    return new Promise<GraphResponse>((resolve, reject) => {
        get<GraphResponse>(`/gp/graph/getGraph`).then((res) => {
            console.debug('获取知识图谱成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('获取知识图谱失败', error);
            reject(error);
        });
    });
}

// 根据实体ID获取知识图谱
export function getGraphByEntityId(entityId: string) {
    return new Promise<GraphResponse>((resolve, reject) => {
        get<GraphResponse>(`/gp/graph/getGraphByEntityId?entityId=${entityId}`).then((res) => {
            console.debug('根据实体ID获取知识图谱成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('根据实体ID获取知识图谱失败', error);
            reject(error);
        });
    });
}

// 获取意识形态教育链接
export function getIdeologyEducationLinks(name: string) {
    return new Promise<any>((resolve, reject) => {
        get<any>(`/gp/graph/ideologyEducationLinks?name=${encodeURIComponent(name)}`).then((res) => {
            console.debug('获取意识形态教育链接成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('获取意识形态教育链接失败', error);
            reject(error);
        });
    });
}

// 知识图谱搜索接口
export interface SearchDocumentItem {
    entityId: string
    name: string
    // 其他可能的字段
    [key: string]: any
}

export interface SearchDocumentResponse {
    code: number
    data: SearchDocumentItem[]
    message: string
    status: number
    tranceId: string
}

export function getDocumentByName(name: string) {
    return new Promise<SearchDocumentResponse>((resolve, reject) => {
        get<SearchDocumentResponse>(`/gp/graph/getDocumentByName?name=${encodeURIComponent(name)}`).then((res) => {
            console.debug('搜索知识图谱文档成功', res);
            resolve(res);
        }).catch((error) => {
            console.error('搜索知识图谱文档失败', error);
            reject(error);
        });
    });
}